package vip.meeet.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public final class ConnectionHelper {

    private static final String BASE = "jdbc:mysql://127.0.0.1:3306/%s?useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8&zeroDateTimeBehavior=round";

    private static final String URL = "jdbc:mysql://127.0.0.1:3306/information_schema?useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8&zeroDateTimeBehavior=round";

    private static final String USER = "tim";

    private static final String PASSWORD = "123456";

    private static final String DDL_USER= "root";
    private static final String DDL_PASSWORD = "654321";

    public static Connection getConnection() throws SQLException {
//        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
        return connection;
    }

    public static Connection getConnection(String user,String password) throws SQLException {
//        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(URL, user, password);
        return connection;
    }

    public static Connection getConnection(String db,String user,String password) throws SQLException {
//        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(String.format(BASE,db), user, password);
        return connection;
    }

    public static Connection getConnection(String db) throws SQLException {
//        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(String.format(BASE,db), USER, PASSWORD);
        return connection;
    }

    public static Connection getDDLConnection(String db) throws SQLException {
        Connection connection = DriverManager.getConnection(String.format(BASE,db), DDL_USER, DDL_PASSWORD);
        return connection;
    }

    public static void exeSql(String db,String sql) throws SQLException, ClassNotFoundException {
        Connection connection = getConnection(db);
        Statement statement = connection.createStatement();
        statement.executeUpdate(sql);
    }

    public static Connection getInformationSchemaConnection() throws SQLException, ClassNotFoundException {
        return getConnection("information_schema");
    }

    public static List<String> getAllTableNames(Connection informationSchemaConnection) throws SQLException {
        String sql = "select distinct TABLE_NAME from tables";
        return DBHelper.getStringList(informationSchemaConnection,sql);
    }

    public static List<String> getSpecificDbTableNames(String db) throws SQLException, ClassNotFoundException {
        String sqlBase = "select distinct TABLE_NAME from tables WHERE table_schema = '%s'";
        String sql = String.format(sqlBase, db);
        Connection connection = getInformationSchemaConnection();
        return DBHelper.getStringList(connection,sql);
    }
}
